﻿

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoEntry
    {
        public string className = "daoEntry";
        public daoEntry()
        {

        }
        private Entry Load(IDataReader de)
        {
            Entry item = new Entry();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.EntryId = clsConvert.DbToInt("EntryId", de);
            item.OrderDetailId = clsConvert.DbToInt("OrderDetailId", de);
            item.Quantity = clsConvert.DbToInt("Quantity", de);
            item.LocationId = clsConvert.DbToInt("LocationId", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.ModifiedBy = clsConvert.DbToString("ModifiedBy", de);
            item.ModifiedDate = clsConvert.DbToDateTime("ModifiedDate", de);
            item.ProducedDate = clsConvert.DbToDateTime("ProducedDate", de);
            item.ProducerId = clsConvert.DbToGuid("ProducerId", de);
            item.SupervisorId = clsConvert.DbToGuid("SupervisorId", de);
            item.ShiftId = clsConvert.DbToInt("ShiftId", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }
        public Entry GetByID(int entryId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Entry item = new Entry();

            try
            {
                para[0] = new SqlParameter("EntryId", SqlDbType.Int);
                para[0].Value = entryId;
                using (de = clsSQL.SPDataReader("Entry_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog(className, "GetByID(int entryId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public bool Delete(int entryId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter("EntryId", SqlDbType.Int);
                para[0].Value = entryId;
                result = clsSQL.SPExecute("Entry_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Delete(int entryId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public int Insert(Entry objEntry)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[9];
            try
            {
                para[0] = new SqlParameter("OrderDetailId", SqlDbType.Int);
                para[0].Value = objEntry.OrderDetailId;
                para[1] = new SqlParameter("Quantity", SqlDbType.Int);
                para[1].Value = objEntry.Quantity;
                para[2] = new SqlParameter("LocationId", SqlDbType.Int);
                para[2].Value = clsConvert.IntToDB(objEntry.LocationId);
                para[3] = new SqlParameter("ProducedDate", SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objEntry.ProducedDate);
                para[4] = new SqlParameter("ProducerId", SqlDbType.UniqueIdentifier);
                para[4].Value = clsConvert.GuidToDB(objEntry.ProducerId);
                para[5] = new SqlParameter("SupervisorId", SqlDbType.UniqueIdentifier);
                para[5].Value = clsConvert.GuidToDB(objEntry.SupervisorId);
                para[6] = new SqlParameter("ShiftId", SqlDbType.Int);
                para[6].Value = clsConvert.IntToDB(objEntry.ShiftId);
                para[7] = new SqlParameter("CreatedDate", SqlDbType.DateTime);
                para[7].Value = clsConvert.DateToDB(objEntry.CreatedDate);
                para[8] = new SqlParameter("CreatedBy", SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objEntry.CreatedBy);

                result = clsSQL.SPExecute("Entry_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Insert(Entry objEntry)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public bool Update(Entry objEntry)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];
            try
            {
                para[0] = new SqlParameter("OrderDetailId", SqlDbType.Int);
                para[0].Value = objEntry.OrderDetailId;
                para[1] = new SqlParameter("Quantity", SqlDbType.Int);
                para[1].Value = objEntry.Quantity;
                para[2] = new SqlParameter("LocationId", SqlDbType.Int);
                para[2].Value = clsConvert.IntToDB(objEntry.LocationId);
                para[3] = new SqlParameter("ProducedDate", SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objEntry.ProducedDate);
                para[4] = new SqlParameter("ProducerId", SqlDbType.UniqueIdentifier);
                para[4].Value = clsConvert.GuidToDB(objEntry.ProducerId);
                para[5] = new SqlParameter("SupervisorId", SqlDbType.UniqueIdentifier);
                para[5].Value = clsConvert.GuidToDB(objEntry.SupervisorId);
                para[6] = new SqlParameter("ShiftId", SqlDbType.Int);
                para[6].Value = clsConvert.IntToDB(objEntry.ShiftId);

                para[7] = new SqlParameter("ModifiedDate", SqlDbType.DateTime);
                para[7].Value = clsConvert.DateToDB(objEntry.ModifiedDate);
                para[8] = new SqlParameter("ModifiedBy", SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objEntry.ModifiedBy);
                para[9] = new SqlParameter("EntryId", SqlDbType.Int);
                para[9].Value = objEntry.EntryId;
                result = clsSQL.SPExecute("Entry_Update", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Update(Entry objEntry)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result == 1);
        }
        public EntryCollection List(Entry objEntry)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            EntryCollection items = new EntryCollection();
            try
            {
                para[0] = new SqlParameter(busEntry.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objEntry.OrderBy;
                para[1] = new SqlParameter(busEntry.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objEntry.OrderDirection;
                para[2] = new SqlParameter(busEntry.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objEntry.CurrentPage;
                para[3] = new SqlParameter(busEntry.PAGESIZE, SqlDbType.Int);
                para[3].Value = objEntry.PageSize;
                para[4] = new SqlParameter(busEntry.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objEntry.Condition;
                para[5] = new SqlParameter(busEntry.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Entry_List", para);
                while (de.Read())
                {
                    Entry item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objEntry.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoEntry", "List(Entry objEntry)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
    }
}
